//--personalCard.js
//获取应用实例
const app = getApp()
var util = require('../../../../utils/util.js')
var _this
Page({
  data: {
    headImg: '../../../../resources/user.jpg',
    user01_img: '../../../../resources/user01.jpg',
    userName: 'Xx',
    userInfo: '这里是个人介绍',
    id: 0,
    list: [{}, {}],
    myInfo: {},
    birdthDay: '',
    // birdthDay: noteBirdthday,
    dateSelected: 'unselectedColor',
    isDatePikcerShow: false,
    date: util.formatTime(new Date),
    common: true,
    filePath: ''
  },
  //事件处理函数
  showDatePicker: function () {
    this.setData({
      isDatePikcerShow: true
    })
    console.log(this.data.isDatePikcerShow)
  },
  enterMather(e) {
    let name = e.currentTarget.dataset.page
    let value = e.currentTarget.dataset.value
    let userId = _this.data.id
    if (name == 'editTag' && userId == 0) {
      value = JSON.stringify(e.currentTarget.dataset.value)
    }
    if (name == 'commonFriends') { 
      wx.navigateTo({
        url: '../' + name + '/' + name + '?value=' + value
      })
    } else if (name !== 'commonFriends' && userId == 0) {
      let apitype = e.currentTarget.dataset.apitype
      let customtag = e.currentTarget.dataset.customtag
      wx.navigateTo({
        url: '../edit/' + name + '/' + name + '?value=' + value + '&apiType=' + apitype + '&customtag=' + customtag
      })
    }
  },
  bindDateChange(e) {
    _this.setData({
      birdthDay: e.detail.value
    })
    _this.handleUpdate()
  },
  onLoad: function (options) {
    _this = this
    let userId = this.data.id
    let isCommon = _this.data.common
    if (options) {
      userId = options.id ? options.id : 
      isCommon = options.common == 'false' ? false : true
    }
    _this.setData({
      id: userId,
      common: isCommon
    })
    if (userId == 0) {
      util.getUserInfo(_this.requestDidResponse, _this.requestDidFaild)
    } else {
      util.getUserInfo(_this.requestDidResponse, _this.requestDidFaild, userId)
    }
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  handleUpdate: () => {
    var data = { 'birthday': _this.data.birdthDay }
    util.updateUserInfo(_this.requestDidResponse, _this.requestDidFaild, data)
  },
  requestDidResponse: (res) => {
    console.log(res)
    if (res.status == '200') {
      if (res.data.target1 == null || res.data.target1.trim() == '') {
        res.data.target1 = '这个人太懒啦， 还没有填写个性签名哦'
      }
      _this.setData({
        myInfo: res.data
      })
      app.globalData['tagSelectedArray1'] = res.data.labels2
      app.globalData['tagSelectedArray2'] = res.data.labels3
      app.globalData['tagSelectedArray3'] = res.data.labels
      app.globalData['customTagSelectedArray1'] = res.data.otherLabels2Arr
      app.globalData['customTagSelectedArray2'] = res.data.otherLabels3Arr
      app.globalData['customTagSelectedArray3'] = res.data.otherLabelsArr
    }
  },
  requestDidFaild: (res) => {

  },
  onShow () { 
    _this.onLoad() 
  },
  chooseImage: () => {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        _this.setData({
          filePath: tempFilePaths
        })
        util.fileUpload(_this.requestDidPic, _this.requestDidPicFaild, _this.data.filePath[0])
        wx.showLoading({
          title: '正在设置...',
        })
      },
      fail() {
      }
    })
  },
  requestDidPic: (res) => {
    wx.hideLoading()
    var data = JSON.parse(res)
    if (data.status == '200') {
      _this.data.myInfo.photoUrl = _this.data.filePath
      _this.setData({
        myInfo: _this.data.myInfo
      })
      wx.showToast({
        title: '修改成功',
        icon: 'success'
      })
      let pages = getCurrentPages() //页面指针数组 
      let prepage = pages[pages.length - 2] //上一页面指针 
      prepage.setData({
        myInfo: _this.data.myInfo
      })
    } else {
      wx.showToast({
        title: '上传失败',
        icon: 'none'
      })
    }
  },
  requestDidPicFaild: (res) => {
    wx.hideLoading()
    console.log(res)
    wx.showToast({
      title: '网络走神了，请稍候再试哦',
      icon: 'none'
    })
  } 
})
